package study.elasticsearch.repository.impl;

import factory.SourceInfo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import study.elasticsearch.repository.BbsRepository;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

@Repository
public class BbsImpl implements BbsRepository {
    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Override
    public String index(SourceInfo info) {
        IndexRequest indexRequest = new IndexRequest(info.getIndex(),info.getType(),info.getId());
        indexRequest.source(info.getSource(),XContentType.JSON);
        try {
            IndexResponse index = restHighLevelClient.index(indexRequest);
            return index.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String get(SourceInfo info) {
        GetRequest getRequest = new GetRequest(info.getIndex(),info.getType(),info.getId());
        // 查询指定的字段
        String[] include = {"tid","title"};
        FetchSourceContext fetchSourceContext = new FetchSourceContext(true,include,null);
        getRequest.fetchSourceContext(fetchSourceContext);
        try {
            // 同步
            GetResponse response = restHighLevelClient.get(getRequest);
            // 获取指定字段值
            // String tid = response.getField("tid").getValue();

            // 异步
           /* restHighLevelClient.getAsync(getRequest, new ActionListener<GetResponse>() {
                @Override
                public void onResponse(GetResponse documentFields) {
                    System.out.println(documentFields.toString());
                }

                @Override
                public void onFailure(Exception e) {

                }
            });*/
            return response.getSourceAsString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean exist(SourceInfo info) {
        GetRequest getRequest = new GetRequest(info.getIndex(),info.getType(),info.getId());
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");
        try {
            boolean result = restHighLevelClient.exists(getRequest);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public String delete(SourceInfo info) {
        DeleteRequest deleteRequest = new DeleteRequest(info.getIndex(),info.getType(),info.getId());
        try {
            DeleteResponse delete = restHighLevelClient.delete(deleteRequest);
            return delete.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String update(SourceInfo info) {
        UpdateRequest updateRequest = new UpdateRequest(info.getIndex(),info.getType(),info.getId());
        // 文档更新方式
        // updateRequest.doc(info.getSource(),XContentType.JSON);
        // 不存在将source插入
        // updateRequest.docAsUpsert(true);
        Map<String,Object> prams = new HashMap<String, Object>(1);
        prams.put("count",4);
        Script script = new Script(ScriptType.INLINE,"painless","ctx._source.tid += params.count",prams);
        updateRequest.script(script);
        updateRequest.scriptedUpsert(false);
        try {
            UpdateResponse update = restHighLevelClient.update(updateRequest);
            return update.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer bulk(SourceInfo info) {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new UpdateRequest(info.getIndex(),info.getType(),info.getId()).doc(XContentType.JSON,"tid","100100"));
        try {
            BulkResponse bulk = restHighLevelClient.bulk(bulkRequest);
            return bulk.status().getStatus();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
